package com.cloud.lan.integration.submail.sms.service.impl;

import com.alibaba.fastjson.JSON;
import com.cloud.lan.integration.submail.common.CommonService;
import com.cloud.lan.integration.submail.common.SubMailConstants;
import com.cloud.lan.integration.submail.sms.entity.SmsSendReq;
import com.cloud.lan.integration.submail.sms.entity.SmsSendRes;
import com.cloud.lan.integration.submail.sms.service.SmsSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.TreeMap;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

/**
 * Created by zhouquan on 2022/4/2.
 */
@Slf4j
@Service
public class SmsSendServiceImpl implements SmsSendService {

    @Autowired
    private CommonService commonService;

    @Override
    public List<SmsSendRes> multixsend(SmsSendReq smsSendReq) {
        try{
            TreeMap<String, String> requestData = new TreeMap<String, String>();
            //组合请求数据
            requestData.put("appid", SubMailConstants.APPID);
            requestData.put("project", smsSendReq.getTemplateId());
            commonService.buildSignature(SubMailConstants.APPID, SubMailConstants.APPKEY, SubMailConstants.SIGNTYPE, requestData);
            requestData.put("multi", JSON.toJSONString(smsSendReq.getModelList()));

            //post请求
            HttpPost httpPost = new HttpPost(SubMailConstants.SMS_MULTIX_SEND_URL);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity entity = new StringEntity(JSONObject.toJSONString(requestData), "UTF-8");
            httpPost.setEntity(entity);
            log.info("短信发送.模板方式批量发送短信.req:[{}]",JSON.toJSONString(httpPost));
            String httpRes = commonService.httpRequest(httpPost);
            List<SmsSendRes> resList = JSON.parseArray(httpRes,SmsSendRes.class);
            log.info("短信发送.模板方式批量发送短信.res:[{}]",JSON.toJSONString(resList));
            return resList;
        }catch (Exception e){
            log.info("短信发送.模板方式批量发送短信.error",e);
        }
        return null;
    }

}

